/*
 * Copyright (C) 2012 Roman Yakovenko <b179a9@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.b17.net.core;

import com.b17.crypto.core.Utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

/**
 * Created by b17
 * 3/3/12 6:09 PM
 */

public class Packet {

    public static final int FILE_NAME_HEADER = 64;
    public static final int FILE_SIZE_HEADER = 4;
    private String fileName;

    private int fileSize;

    private byte[] data;

    public Packet(File file) throws IOException {
        fileName = file.getName();
        InputStream stream = new FileInputStream(file);
        fileSize = stream.available();
        data = new byte[fileSize];
        stream.read(data);
    }

    protected Packet(byte[] fileName, byte[] fileSize, byte[] data) {
        this.fileName = new String(fileName);
        this.fileSize = Utils.toInteger(fileSize);
        this.data = data;
    }

    public String getFileName() {
        return fileName;
    }

    public int getFileSize() {
        return fileSize;
    }

    public byte[] getData() {
        return data;
    }

    public byte[] toByte() {
        byte[] fileNameHeader = new byte[FILE_NAME_HEADER];
        byte[] fileName = this.fileName.getBytes();
        System.arraycopy(fileName, 0, fileNameHeader, 0, Math.min(fileNameHeader.length, fileName.length));
        byte[] fileSizeHeader = Utils.toBytes(fileSize);
        byte[] packet = new byte[fileNameHeader.length + fileName.length + data.length];
        System.arraycopy(fileNameHeader, 0, packet, 0, fileNameHeader.length);
        System.arraycopy(fileSizeHeader, 0, packet, fileNameHeader.length, fileSizeHeader.length);
        System.arraycopy(data, 0, packet, fileNameHeader.length + fileSizeHeader.length, data.length);
        return packet;
    }

    public static Packet create(byte[] packetData) {
        byte[] fileNameHeader = Arrays.copyOfRange(packetData, 0, FILE_NAME_HEADER);
        byte[] fileSizeHeader = Arrays.copyOfRange(packetData, FILE_NAME_HEADER, FILE_NAME_HEADER + FILE_SIZE_HEADER);
        byte[] fileData = Arrays.copyOfRange(packetData, FILE_NAME_HEADER + FILE_SIZE_HEADER,
                packetData.length);
        return new Packet(fileNameHeader, fileSizeHeader, fileData);
    }
}
